home *** CD-ROM | disk | FTP | other *** search
/ Video Toaster 4.2 / Video Toaster v4.2.iso / programs / documentation / lightwave / sdk / sample / negative / negative.c next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  4.0 KB  |  193 lines

  1. /*
  2.  * NEGATIVE.C -- Layout Plugin Post-Process Filter
  3.  *         Invert color of image after rendering
  4.  *
  5.  * by Allen Hastings and Arnie Cachelin
  6.  * revised by Stuart Ferguson
  7.  * last revision  6/9/95
  8.  */
  9. #include <splug.h>
  10. #include <moni.h>
  11. #include <lwran.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <math.h>
  15.  
  16.  
  17.  
  18. /*
  19.  * HANDLER -- Instance Methods
  20.  *
  21.  * An instance is a particular set of user-set options for a plug-in
  22.  * operation.  Normally the host would call these callback functions
  23.  * to manage instances, but since the Negative image filter has no
  24.  * options, only a dummy instance needs to be used.  In this case the
  25.  * pointer for all instances will just be a pointer to a string and 
  26.  * the instance handler functions will do nothing.
  27.  */
  28. static char         dummyInst[] = "NIL";
  29.  
  30.  
  31.     XCALL_(LWInstance)
  32. NilCreate (
  33.     LWError            *err)
  34. {
  35.     return ((LWInstance) dummyInst);
  36. }
  37.  
  38.     XCALL_(void)
  39. NilDestroy (
  40.     LWInstance         inst)
  41. {
  42. }
  43.  
  44.     XCALL_(LWError)
  45. NilCopy (
  46.     LWInstance         from,
  47.     LWInstance         to)
  48. {
  49.     return ((LWInstance) dummyInst);
  50. }
  51.  
  52.     XCALL_(LWError)
  53. NilLoad (
  54.     LWInstance         inst,
  55.     const LWLoadState    *lState)
  56. {
  57.     return (NULL);
  58. }
  59.  
  60.     XCALL_(LWError)
  61. NilSave (
  62.     LWInstance         inst,
  63.     const LWSaveState    *sState)
  64. {
  65.     return (NULL);
  66. }
  67.  
  68.  
  69.  
  70. /*
  71.  * PROCESSING
  72.  *
  73.  * The host calls this function to process each rendered frame.  The
  74.  * server has to process the whole frame, line-by-line.  In this case
  75.  * the red, green and blue buffers are read and inverted before being
  76.  * set back, and the alpha buffer is simply copied.  All pixels must
  77.  * be touched even if only a few or none are changed.
  78.  *
  79.  * The monitor in the filter access is used to track the progress of
  80.  * the filter for the user and to check for user abort.  If this is
  81.  * used with the pre-release, the monitor is not present in the
  82.  * access struct and is nulled.
  83.  */
  84.     XCALL_(static void)
  85. NegProcess (
  86.     LWInstance         inst,
  87.     const FilterAccess    *fa)
  88. {
  89.     Monitor            *mon;
  90.     BufferValue         out[4], *r, *g, *b, *a;
  91.     int             i, j;
  92.  
  93. #ifdef LW_PRERELEASE
  94.     mon = NULL;
  95. #else
  96.     mon = fa->monitor;
  97. #endif
  98.  
  99.     /*
  100.      * Scan through the lines in the image and get image buffer
  101.      * line pointers for the RGB and alpha buffers of the original
  102.      * image.
  103.      */
  104.     MON_INIT (mon, fa->height);
  105.     for (i = 0; i < fa->height; i++) {
  106.         r = (*fa->bufLine) (LWBUF_RED,   i);
  107.         g = (*fa->bufLine) (LWBUF_GREEN, i);
  108.         b = (*fa->bufLine) (LWBUF_BLUE,  i);
  109.         a = (*fa->bufLine) (LWBUF_ALPHA, i);
  110.  
  111.         /*
  112.          * For each pixel in the current line, compute the
  113.          * negative color value and leave the alpha alone.
  114.          */
  115.         for (j = 0; j < fa->width; j++) {
  116.             out[0] = 255 - r[j];
  117.             out[1] = 255 - g[j];
  118.             out[2] = 255 - b[j];
  119.             out[3] = a[j];
  120.  
  121.             (*fa->setRGB)   (j, i, out);
  122.             (*fa->setAlpha) (j, i, out[3]);
  123.         }
  124.  
  125.         /*
  126.          * Step the monitor and check for abort on each line.
  127.          * This is a good compromise of responsiveness and
  128.          * input-checking overhead.
  129.          */
  130.         if (MON_STEP (mon))
  131.             return;
  132.     }
  133.  
  134.     MON_DONE (mon);
  135. }
  136.  
  137.  
  138.  
  139. /*
  140.  * FLAGS
  141.  *
  142.  * The host calls this function to determine what options are needed by
  143.  * this instance of filter.  Specifically, which buffers other than the
  144.  * normal RGBA buffers need to be computed for this filter.  Since the
  145.  * negative operation olny operates on colors, no additional buffers
  146.  * are needed.
  147.  */
  148.     XCALL_(static unsigned int)
  149. NegFlags (
  150.     LWInstance         inst)
  151. {
  152.     return (0);
  153. }
  154.  
  155.  
  156.  
  157. /*
  158.  * ACTIVATE -- Main entry point
  159.  *
  160.  * The host calls this function with an uninitialized image filter handler
  161.  * and the server has to fill in the callback fields for the handler
  162.  * functions for this handler class.
  163.  */
  164.     XCALL_(int)
  165. Activate (
  166.     long             version,
  167.     GlobalFunc        *global,
  168.     ImageFilterHandler    *local,
  169.     void            *serverData)
  170. {
  171.     XCALL_INIT;
  172.     if (version != 1)
  173.         return (AFUNC_BADVERSION);
  174.  
  175.     local->inst.create  = NilCreate;
  176.     local->inst.destroy = NilDestroy;
  177.     local->inst.load    = NilLoad;
  178.     local->inst.save    = NilSave;
  179.     local->inst.copy    = NilCopy;
  180.  
  181.     local->process = NegProcess;
  182.     local->flags   = NegFlags;
  183.  
  184.     return (AFUNC_OK);
  185. }
  186.  
  187.  
  188. /*
  189.  * Globals necessary to declare the class and name of this plugin server.
  190.  */
  191. char        ServerClass[] = "ImageFilterHandler";
  192. char        ServerName[]  = "Demo_Negative";
  193.